/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlysieuthi;
import DAO.*;
import DTO.*;
import java.util.ArrayList;
import java.util.List;
import Model.CModel;
import java.awt.geom.Arc2D;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Khang
 */
public class FrmGiaBan extends javax.swing.JFrame {

    
    /**
     * Creates new form FrmGiaBan
     */
    public FrmGiaBan() {
        initComponents();
        this.loadDataGiaBan();
    }
    List<CGiaBan> lgiaBan = new ArrayList<CGiaBan>();
    DecimalFormat df = new DecimalFormat("#,###");        
    private void loadDataGiaBan(){
        CModel _model = new CModel(QuanLySieuThi.user_name);
        lgiaBan = _model.getGiaBan();
        _model.saveHisTory();
        _model.Close();
        
        Vector<Vector> data = new Vector<Vector>();
        for(CGiaBan item : lgiaBan){
            Vector<String> row = new Vector<String>();        
            row.addElement("" + item.getTenSanPham());
            row.addElement(df.format(item.getGiaNhap())); 
            row.addElement(df.format(item.getGiaBanKHVL()));
            data.add(row);
        }
        Vector<String> columnNames = new Vector<String>();
        //columnNames.addElement("ID");
       // columnNames.addElement("san pham id");
        columnNames.addElement("Tên sản phẩm");
        columnNames.addElement("Giá nhập");
        //columnNames.addElement("Giá bán sỉ");
        columnNames.addElement("Giá bán lẻ");        
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        this.tbDanhSach.removeAll();
        this.tbDanhSach.setModel(model);
        this.tbDanhSach.setFillsViewportHeight(true);
        //this.tbDanhSach.getColumnModel().getColumn(0).setMinWidth(0);
        //this.tbDanhSach.getColumnModel().getColumn(0).setMaxWidth(0);
        //this.tbDanhSach.getColumnModel().getColumn(1).setMinWidth(0);
        //this.tbDanhSach.getColumnModel().getColumn(1).setMaxWidth(0);
    }
    
    private void updateGia(){
        SubConfig config = SubConfig.getInstance();
        config.init();
        CGiaBanDAO gbDAO = new CGiaBanDAO();
        List<CHistory> lHistory = new ArrayList<CHistory>();
        CHistoryDAO historyDao = new CHistoryDAO();
        for(int i = 0;i < this.tbDanhSach.getRowCount();i++){
            CGiaBan gb = lgiaBan.get(i);
            //String path = table.getModel().getValueAt(rowIndex, 1).toString();
            double rs = 0.0;
            String value = this.tbDanhSach.getModel().getValueAt(i, 1).toString();
            System.out.println("gia nhap : " + value);
            value = value.replaceAll(",", "");
            rs = Double.parseDouble(value);
            gb.setGiaNhap(rs);
            //value = this.tbDanhSach.getModel().getValueAt(i, 2).toString();
            Object obj = this.tbDanhSach.getModel().getValueAt(i, 2).toString();
            System.out.println(String.format("gia ban %s", obj));
            if(obj!= null){
                value = obj.toString();
                value = value.replaceAll(",", "");
                rs = Double.parseDouble(value);
            }
            
            gb.setGiaBanKHVL(rs);
            gb.setNguoiSua(QuanLySieuThi.user_name);
             CHistory history = new CHistory();
            history.setChuc_nang("thay doi gia san pham");
            history.setMota(gb.toString());
            history.setNguoiTao(QuanLySieuThi.user_name);
            lHistory.add(history);
        }
        
        try {
            gbDAO.updateRows(lgiaBan, config);
            historyDao.addrows(lHistory, config);
        } catch (SQLException ex) {
            Logger.getLogger(FrmGiaBan.class.getName()).log(Level.SEVERE, null, ex);
        }
        config.close();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNgayNhap1 = new javax.swing.JLabel();
        txtNgayNhap = new javax.swing.JTextField();
        lblNhanVienNhap = new javax.swing.JLabel();
        txtNVNhap = new javax.swing.JTextField();
        lblNgayNhap2 = new javax.swing.JLabel();
        txtNgayNhap1 = new javax.swing.JTextField();
        lblNhanVienNhap1 = new javax.swing.JLabel();
        txtNVNhap1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSach = new javax.swing.JTable();
        btnGhi = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        lblNgayNhap1.setText("Ngày nhập:");

        lblNhanVienNhap.setText("Nhân viên nhập:");

        lblNgayNhap2.setText("Ngày nhập:");

        lblNhanVienNhap1.setText("Nhân viên nhập:");

        tbDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbDanhSach);

        btnGhi.setText("Ghi");
        btnGhi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGhiActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(102, 102, 255));
        jLabel1.setText("BẢNG GHI NHẬN GIÁ BÁN CỦA TỪNG SẢN PHẨM");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 795, Short.MAX_VALUE)
                    .addComponent(btnGhi, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(btnGhi)
                .addGap(13, 13, 13))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnGhiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGhiActionPerformed
        // TODO add your handling code here:
        updateGia();
    }//GEN-LAST:event_btnGhiActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmGiaBan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmGiaBan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmGiaBan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmGiaBan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmGiaBan().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGhi;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNgayNhap1;
    private javax.swing.JLabel lblNgayNhap2;
    private javax.swing.JLabel lblNhanVienNhap;
    private javax.swing.JLabel lblNhanVienNhap1;
    private javax.swing.JTable tbDanhSach;
    private javax.swing.JTextField txtNVNhap;
    private javax.swing.JTextField txtNVNhap1;
    private javax.swing.JTextField txtNgayNhap;
    private javax.swing.JTextField txtNgayNhap1;
    // End of variables declaration//GEN-END:variables
}
